********************************************************************************
*Replication files for "Purging to transform the post-colonial state: Evidence
*from the 1952 Egyptian Revolution"
********************************************************************************

cd "your cd here" 
set more off

log using "ketchley_wenig_CPS_log.txt", replace

********************************************************************************
*Packages to install
*ssc install palettes, replace
*ssc install colrspace, replace
*ssc install heatmap, replace
*ssc install coefplot, replace
********************************************************************************

*Figure 2
import delimited "fig2_FO_ranks.csv", clear 

twoway bar pct_rank order, xlab(1 "Field Marshal" 2 "Lieutenant General" 3 "Major General" 4 "Brigadier General" 5 "Colonel" 6 "Lieutenant Colonel" 7 "Major" 8 "Captain" 9 "First Lieutenant" 10 "Second Lieutenant", angle(45) grid) xtitle("") plotregion(margin(0 0 0 2)) ytitle("Free Officers (%)") ylab(, grid) scheme(lean1)

graph export "figures\fig2.png", replace as(png) 

*Figure 3
import delimited "fig3_library_catalog.csv", clear 

twoway bar pct_copies order, xlab(1 "Literature" 2 "Military science" 3 "Military history" 4 "Science/religion" 5 "Dictionaries/references" 6 "Novels" 7 "Memoirs/biographies" 8 "Magazines/periodicals" 9 "History" 10 "Geography", angle(45) grid) xtitle("") plotregion(margin(0 0 0 2)) ytitle("Books in military academy (%)") ylab(, grid) scheme(lean1) yscale(r(., 25)) ylab(0(5)25) graphregion(margin(l+1 r+1)) 

graph export "figures\fig3.png", replace as(png) 

********************************************************************************
*Analysis
********************************************************************************
*Read in master dataset
use ketchley_wenig_CPS, clear

*Figure 4
sts graph, scheme(lean1) xlabel(0(1)6, grid) ytitle("Pr(Survival)") xtitle(Years post-1952) title("") 

graph export "figures\fig4.png", replace as(png) 

*Figure 5 (the output is reproduced in Table A3)
mestreg threat_index competence_index ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh || ministry_ID:, distribution(lognormal) tratio
estat sd

coefplot, eform drop(_cons ln_dist_to_power sheikh nasser ln_individuals_in_ministry ln_mil_experience_in_ministry) xtitle("Survival Time Ratio") levels(95) scheme(lean1) xline(1, lcolor(red)) xlab(0.7 (0.1) 1.3,grid) msymbol(o) msize(medlarge) mcolor(gs6) ciopts(lcol(gs6) lwidth(medthick)) mlabgap(.1) format(%9.2f) mlabposition(12) mlabcolor(gs6) mlabsize(vsmall) mlabel(cond(@pval<.001, "***", ///
            cond(@pval<.01, "**",    ///
            cond(@pval<.05, "*", "")))) ///
note("P-values (two-tailed): *p<.05, **p<.01, ***p<.001" "Controls included: Individuals in ministry, Distance to Abdeen Palace," "Post-1952 Free Officer experience in ministry, Year Nasser takes power, Shaykh") xscale(r(0.7, 1.3))

graph export "figures\fig5.png", replace as(png) 

*Figure 6
sum threat_index, det
local p01 = r(p1)
local p99 = r(p99)

stcurve, survival   ///
at(threat_index=`p01') ///
at(threat_index=`p99') ///
scheme(lean1) xlabel(1(1)6, grid) ylab(0 .2 .4 .6 .8 1,grid) legend(position(6)) legend(cols(3) order(1 "1{superscript:st} percentile" 2 "99{superscript:th} percentile") ring(0.5)) xtitle(Years post-1952) title("Threat index") name(threat_index, replace) aspectratio(1) yscale(r(0, 1))

sum competence_index, det
local p01 = r(p1)
local p99 = r(p99)

stcurve, survival ///
at(competence_index=`p01') ///
at(competence_index=`p99') ///
scheme(lean1) xlabel(1(1)6, grid) ylab(0 .2 .4 .6 .8 1,grid) legend(position(6)) legend(cols(3) order(1 "1{superscript:st} percentile" 2 "99{superscript:th} percentile") ring(0.5)) xtitle(Years post-1952) title("Competence index") name(competence_index, replace) aspectratio(1) yscale(r(0, 1)) 

grc1leg threat_index competence_index, cols(2) scheme(lean1) legendfrom(competence_index)

graph export "figures\fig6.png", replace as(png) 

*Figure 7 (the output is reproduced in Table A4)
mestreg threat_index competence_index threat_competence ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh || ministry_ID:, distribution(lognormal) tratio cformat(%9.3f)
estat sd

coefplot, eform drop(_cons ln_dist_to_power sheikh nasser ln_individuals_in_ministry ln_mil_experience_in_ministry) xtitle("Survival Time Ratio") levels(95) scheme(lean1) xline(1, lcolor(red)) xlab(0.7 (0.1) 1.3,grid) msymbol(o) msize(medlarge) mcolor(gs6) ciopts(lcol(gs6) lwidth(medthick)) mlabgap(.1) format(%9.2f) mlabposition(12) mlabcolor(gs6) mlabsize(vsmall) mlabel(cond(@pval<.001, "***", ///
            cond(@pval<.01, "**",    ///
            cond(@pval<.05, "*", "")))) ///
note("P-values (two-tailed): *p<.05, **p<.01, ***p<.001" "Controls included: Individuals in ministry, Distance to Abdeen Palace," "Post-1952 Free Officer experience in ministry, Year Nasser takes power, Shaykh") xscale(r(0.7, 1.3))

graph export "figures\fig7.png", replace as(png) 

*Figure 8
sum threat_competence, det
local threat_competence_p99 = r(p99)
local threat_competence_p1 = r(p1)

stcurve, survival ///
at(threat_competence=`threat_competence_p1') ///
at(threat_competence=`threat_competence_p99') ///
scheme(lean1) xlabel(1(1)6, grid) ylab(0 .2 .4 .6 .8 1,grid) legend(position(6)) legend(cols(1) order(1 "1{superscript:st} percentile" 2 "99{superscript:th} percentile") ring(0.5)) xtitle(Years post-1952) title("Interaction term") aspectratio(1) yscale(r(0, 1)) 

graph export "figures\fig8.png", replace as(png) 

*Figure 9 (the output is reproduced in Table A5)
mestreg senior_official ln_royal_orders pasha_bey military_officer positions ln_years_in_office_since_1939 university_education ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh || ministry_ID:, distribution(lognormal) tratio
estat sd

coefplot, eform drop(_cons ln_dist_to_power sheikh nasser ln_individuals_in_ministry ln_mil_experience_in_ministry) xtitle("Survival Time Ratio") levels(95) scheme(lean1) xline(1, lcolor(red)) xlab(0.7(.1)1.3,grid) msymbol(o) msize(medlarge) mcolor(gs6) ciopts(lcol(gs6) lwidth(medthick)) mlabgap(.1) format(%9.2f) mlabposition(12) mlabcolor(gs6) mlabsize(vsmall) mlabel(cond(@pval<.001, "***", ///
            cond(@pval<.01, "**",    ///
            cond(@pval<.05, "*", "")))) ///
note("P-values (two-tailed): *p<.05, **p<.01, ***p<.001" "Controls included: Individuals in ministry," "Distance to Abdeen Palace, Post-1952 Free Officer experience in ministry," "Year Nasser takes power, Shaykh") xscale(r(0.7, 1.3))

graph export "figures\fig9.png", replace as(png) 

*Figure 10
sum ln_years_in_office_since_1939, det
local p01 = r(p1)
local p99 = r(p99)

stcurve, survival ///
at(ln_years_in_office_since_1939=`p01') ///
at(ln_years_in_office_since_1939=`p99') ///
scheme(lean1) xlabel(1(1)6, grid) ylab(0 .2 .4 .6 .8 1,grid) legend(position(6)) legend(cols(2) order(1 "1{superscript:st} percentile" 2 "99{superscript:th} percentile")) xtitle(Years post-1952) title("Experience pre-1952") aspectratio(1) yscale(r(0, 1)) name(years_in_office, replace)

sum ln_royal_orders, det
local p01 = r(p1)
local p99 = r(p99)

stcurve, survival ///
at(ln_royal_orders=`p01') ///
at(ln_royal_orders=`p99') ///
scheme(lean1) xlabel(1(1)6, grid) ylab(0 .2 .4 .6 .8 1,grid) legend(position(6)) legend(cols(2) order(1 "1{superscript:st} percentile" 2 "99{superscript:th} percentile")) xtitle(Years post-1952) title("Royal honors") aspectratio(1) yscale(r(0, 1)) name(royal_honors, replace)

grc1leg royal_honors years_in_office, cols(2) scheme(lean1) legendfrom(years_in_office)

graph export "figures\fig10.png", replace as(png) 


*Figure 11
stcurve, survival ///
at(positions=1) ///
at(positions=2) ///
at(positions=3) ///
at(positions=4) ///
scheme(lean1) xlabel(1(1)6, grid) ylab(0 .2 .4 .6 .8 1,grid) legend(position(6)) legend(cols(2) order(1 "1" 2 "2" 3 "3" 4 "4")) xtitle(Years post-1952) title("Positions") aspectratio(1)

graph export "figures\fig11.png", replace as(png) 

*Figure 12
mestreg senior_official ln_royal_orders pasha_bey military_officer positions ln_years_in_office_since_1939 university_education ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh || ministry_ID:, distribution(lognormal) tratio

margins, predict(mean marginal) at(university_education=1 ln_years_in_office_since_1939=(0(.5)3) senior_official=0 ln_royal_orders=0 positions=1) noatlegend post 
est store model1_0 

mestreg senior_official ln_royal_orders pasha_bey military_officer positions ln_years_in_office_since_1939 university_education ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh || ministry_ID:, distribution(lognormal) tratio

margins, predict(mean marginal) at(university_education=0 ln_years_in_office_since_1939=(0(.5)3) senior_official=1 ln_royal_orders=2.5 positions=4) noatlegend post 
est store model2_0

coefplot (model1_0, recast(line) lpattern(dash) lwidth(medthick) lcolor(red) ciopt(recast(rarea) color(red%15) alwidth(none))) ///
(model2_0, recast(line) lwidth(medthick) lpattern(dash) lcolor(blue) ciopt(recast(rarea) color(blue%15) alwidth(none))), at xtitle("Years in office since 1939 (log)") ytitle("Mean years surviving in office") ylab(0(1)7, grid) xlab(0 0.5 1 1.5 2 2.5 3, grid) legend(off) text(1 2 "Senior official, no university education, 3 royal honors, 4 positions", color(blue) size(small)) text(4.75 2 "Junior official, university education, 0 royal honors, 1 position", color(red) size(small)) scheme(lean1)

graph export "figures\fig12.png", replace as(png) 

*Figure 13
mestreg senior_official ln_royal_orders pasha_bey military_officer positions ln_years_in_office_since_1939 university_education ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh || ministry_ID:, distribution(lognormal) tratio

predict u0, reffects reses(u0se2)
egen justone=tag(ministry_ID)
sort u0
generate rank=sum(justone)
sort rank
list ministry_ID u0 u0se2 rank if justone==1

br ministry_ID u0 u0se2 rank if justone==1
gen u0_scale = u0se2*1.96
gen u0_ll = u0-u0_scale 
gen u0_ul = u0+u0_scale 

twoway (scatter u0 rank if justone==1, msymbol(o) msize(medlarge) mcolor(gs6)) ///
(rspike u0_ll u0_ul rank if justone==1, vertical lwidth(medthick) lcolor(gs6)), scheme(lean1) ytitle("Institutional effect on survival") yline(0, lcolor(red)) ylab(-1.5 -1 -.5 0 0.5 1 1.5,grid) xlab(,grid) legend(off) xlab(1 "Interior" 2 "War and Navy" 3 "State Councils" 4 "Parliament" 5 "Municipal and Rural Affairs" 6 "Diplomatic Corps" 7 "Social Affairs" 8 "Public Health" 9 "Religious Endowments" 10 "Agriculture" 11 "Communications" 12 "Public Works" 13 "Commerce and Industry" 14 "Justice" 15 "Royal Court" 16 "Finance and Economy" 17 "Supply" 18 "Education" 19 "Foreign Affairs",grid angle(vertical) labsize(small)) xtitle("")

graph export "figures\fig13.png", replace as(png) 


*************************************************************************
*Appendix
*************************************************************************

*Appendix Figure 4
corr threat_index competence_index senior_official ln_royal_orders pasha_bey military_officer positions ln_years_in_office_since_1939 university_education ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh

matrix corrmatrix = r(C)

heatplot corrmatrix, values(format(%4.2f) size(small)) legend(off) color(magma, reverse) xlab(, angle(45)) label

*graph export "figures\figA4.png", replace as(png) 

*Appendix Table A2 - Descriptive statistics
estpost tabstat threat_index competence_index senior_official ln_royal_orders pasha_bey military_officer positions ln_years_in_office_since_1939 university_education  ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh, c(stat) stat(mean sd min max)

*Appendix Table A6 - Random Forest Imputation
*Bring in .csv file created in "appendix_random_forest.R"
preserve
import delimited "im_df.csv", bindquote(strict) clear 
rename sqrt_clubs_membership im_sqrt_clubs_membership
rename age im_age
save im_variables, replace
restore

merge m:1 person_id using im_variables
drop _merge

mestreg senior_official ln_royal_orders pasha_bey military_officer positions ln_years_in_office_since_1939 university_education ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh im_age muslim im_sqrt_clubs_membership shared_surname_FO || ministry_ID:, distribution(lognormal) tratio cformat(%9.3f)

*Appendix Table 7
mestreg threat_index competence_index || ministry_ID:, distribution(lognormal) tratio cformat(%9.3f)
estat sd

*Appendix Table 8
mestreg threat_index competence_index threat_competence || ministry_ID:, distribution(lognormal) tratio cformat(%9.3f)
estat sd

*Appendix Table 9
mestreg senior_official ln_royal_orders pasha_bey military_officer positions ln_years_in_office_since_1939 university_education || ministry_ID:, distribution(lognormal) tratio cformat(%9.3f)

*Appendix Table 10
mestreg threat_index_wo_positions competence_index ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh positions || ministry_ID:, distribution(lognormal) tratio cformat(%9.3f)
estat sd

*Appendix Table 11
mestreg threat_pcf competence_pcf ln_individuals_in_ministry ln_dist_to_power ln_mil_experience_in_ministry nasser sheikh positions || ministry_ID:, distribution(lognormal) tratio cformat(%9.3f)
estat sd


log close
